package com.du.lin.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

    /**
     * 获取当月的 天数
     */
    public static int getCurrentMonthDay()
    {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static int getDaysByYearMonth(int year, int month)
    {
        Calendar a = Calendar.getInstance();
        a.set(Calendar.YEAR, year);
        a.set(Calendar.MONTH, month - 1);
        a.set(Calendar.DATE, 1);
        a.roll(Calendar.DATE, -1);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static int getDaysByYearMonth(Date date)
    {
        Calendar a = Calendar.getInstance();
       a.setTime(date);
        int maxDate = a.getActualMaximum(Calendar.DAY_OF_MONTH);
        return maxDate;
    }

    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static int getDaysByYearMonth(String date)throws Exception
    {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM");
        Calendar a = Calendar.getInstance();
        a.setTime(sdf.parse(date));
        int maxDate = a.getActualMaximum(Calendar.DAY_OF_MONTH);
        return maxDate;
    }

    /**
     * 根据 年、月 获取对应的月份 的 天数
     */
    public static int getDaysByMonth(String date)throws Exception
    {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM");
        Calendar a = Calendar.getInstance();
        a.setTime(sdf.parse(date));
        return  a.get(Calendar.MONTH)+1;
    }

    /**
     *获取当前是几号
     */
    public static int getDayByYearMonthDays(String date)throws Exception
    {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-DD");
        Calendar a = Calendar.getInstance();
        a.setTime(sdf.parse(date));
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }

    /**
     *获取当前是几号
     */
    public static int getDayByYearMonthDays(Date date)throws Exception
    {
        SimpleDateFormat sdf =new SimpleDateFormat("yyyy-MM-DD");
        Calendar a = Calendar.getInstance();
        a.setTime(date);
        int maxDate = a.get(Calendar.DATE);
        return maxDate;
    }
    public static void main(String arg[])throws  Exception{
        System.out.println(getDayByYearMonthDays("2019-08-21"));
        //System.out.println(getDaysByMonth("2019-08"));
    }
}
